package com.sh.data.engine.web.webapi.controller.datadev;

import com.sh.data.engine.application.model.dto.datadev.OfflineDevApiHttpResponse;
import com.sh.data.engine.application.model.dto.datadev.OfflineDevApiWsResponse;
import com.sh.data.engine.application.model.dto.integration.*;
import com.sh.data.engine.application.model.request.datadev.OfflineDevApiHttpTaskSaveRequest;
import com.sh.data.engine.application.model.request.datadev.OfflineDevApiWsTaskSaveRequest;
import com.sh.data.engine.application.model.request.integration.*;
import com.sh.data.engine.application.service.datadev.OfflineDevApiApplicationService;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author : changkong
 * @date : 2022/3/9
 */
@Validated
@Tag(name = "offline接口采集Http-Https")
@RestController
@RequestMapping("/offline/integration/api")
public class OfflineDevApiController {

    @Autowired
    private OfflineDevApiApplicationService offlineDevApiApplicationService;

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/http/getTaskById")
    @Operation(summary = "查看任务")
    public RestResponseEntity<OfflineDevApiHttpResponse> getTaskById(
        @Parameter(description = "任务id") @RequestParam(required = true) Long id) {
        var result = offlineDevApiApplicationService.getTaskById(id);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/ws/getTaskById")
    @Operation(summary = "查看任务")
    public RestResponseEntity<OfflineDevApiWsResponse> getWsTaskById(
        @Parameter(description = "任务id") @RequestParam(required = true) Long id) {
        var result = offlineDevApiApplicationService.getWsTaskById(id);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/http/saveTask")
    @Operation(summary = "新建/保存编辑任务")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.离线开发接口采集)
//    @Log(
//        business = Business.离线开发接口采集,
//        operation = Operation.ADD_UPDATE)
    public RestResponseEntity saveTask(
        @Validated @RequestBody OfflineDevApiHttpTaskSaveRequest saveTaskRequest) {
        var result = offlineDevApiApplicationService.saveTask(saveTaskRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/start")
    @Operation(summary = "启动离线同步任务", description = "启动离线同步任务")
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.离线开发同步任务)
//    @Log(
//        business = Business.离线开发同步任务,
//        operation = Operation.START_TASK)
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1)
    public RestResponseEntity<Long> start(
        @Parameter(description = "nodeId") @RequestParam(value = "nodeId") Long nodeId) {
        var result = offlineDevApiApplicationService.start(nodeId);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/ws/saveTask")
    @Operation(summary = "新建/保存编辑任务")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.离线开发接口采集)
//    @Log(
//        business = Business.离线开发接口采集,
//        operation = Operation.ADD_UPDATE)
    public RestResponseEntity<Boolean> saveTask(
        @Validated @RequestBody OfflineDevApiWsTaskSaveRequest saveTaskRequest) {
        var result = offlineDevApiApplicationService.saveTask(saveTaskRequest);
        return RestResponseEntity.success(result);
    }

    // =====新建任务流程接口 第一步======
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/http/test")
    @Operation(summary = "接口测试")
    public RestResponseEntity<TestApiDTO> test(@Valid @RequestBody TestApiHttpRequest testApiHttpRequest) {
        var result = offlineDevApiApplicationService.test(testApiHttpRequest);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.WEB, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "根据wsdl服务地址获取端口号和方法列表, 供前端下拉框选择")
    @PostMapping(value = "/ws/getWsdlPortOperationMap")
    public RestResponseEntity<GetServiceDTO> getWsdlPortsAndMethods(
        @Valid @RequestBody GetServiceRequest getServiceRequest) {
        var result = offlineDevApiApplicationService.getWsdlPortsAndMethods(getServiceRequest);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/ws/test")
    @Operation(summary = "接口测试")
    public RestResponseEntity<TestApiDTO> test(@Valid @RequestBody TestApiWsRequest testApiwsRequest) {
        var result = offlineDevApiApplicationService.test(testApiwsRequest);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/uploadJar")
    @Operation(summary = "上传jar包")
    public RestResponseEntity<UploadJarDTO> uploadJar(@RequestPart("file") MultipartFile file) {
        var result = offlineDevApiApplicationService.uploadJar(file);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/preview")
    @Operation(summary = "预览")
    public RestResponseEntity<List<List<Object>>> preview(
        @Valid @RequestBody PreviewExpressRequest previewExpressRequest) {
        var result = offlineDevApiApplicationService.preview(previewExpressRequest);
        return RestResponseEntity.success(result);
    }

    // =====新建任务流程接口 第二步选表或建表======
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/getDatabases")
    @Operation(summary = "获取数据目标")
    public RestResponseEntity<GetDataSourceDTO> getDatabases() {
        var result = offlineDevApiApplicationService.getDatabases();
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/getTargetTables")
    @Operation(summary = "获取目标表")
    public RestResponseEntity<GetTargetTablesDTO> getTargetTables(
        @Parameter(description = "数据源Id") @RequestParam(required = true) Long dsId) {
        var result = offlineDevApiApplicationService.getTargetTables(dsId);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/getCreateTableDDL")
    @Operation(summary = "获取一键建表语句")
    public RestResponseEntity<GetCreateTableDDLDTO> getCreateTableDDL(
        @Valid @RequestBody GetCreateTableDDLRequest getCreateTableDDLRequest) {
        var result = offlineDevApiApplicationService.getCreateTableDDL(getCreateTableDDLRequest);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 1, isAdd = 1)
    @PostMapping("/saveTargetTable")
    @Operation(summary = "一键建表")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.TABLE)
    @LogExecution(operation = EngineOperation.DATA_SYNC_CREATE_TABLE, business = Business.离线开发接口采集)
//    @Log(
//        business = Business.离线开发接口采集,
//        operation = Operation.DATA_SYNC_CREATE_TABLE)
    public RestResponseEntity<String> saveTargetTable(
        @Validated @RequestBody SaveTargetTableRequest saveTargetTableRequest) {
        var result = offlineDevApiApplicationService.saveTargetTable(saveTargetTableRequest);
        return RestResponseEntity.success(result);
    }

    // =====新建任务流程接口 第三步配置字段映射======
//    @AuthorityAnnotation(menuId = MenuEnumNew.ZYKF, readWriteStatus = 0, isAdd = 1)
    @GetMapping("/getTagetTableFields")
    @Operation(summary = "获取关联映射字段")
    public RestResponseEntity<GetFieldsDTO> getTargetTableFields(
        @Parameter(description = "数据源Id") @RequestParam(required = true) Long dsId,
        @Parameter(description = "表名") @RequestParam(required = true) String tableName) {
        var result = offlineDevApiApplicationService.getTargetTableFields(dsId, tableName);
        return RestResponseEntity.success(result);
    }
}
